package com.googlecode.microanalyst.designer.model;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.swt.graphics.Image;

import com.googlecode.microanalyst.designer.model.property.PropertyHandler;

public class AnalysisDiagram extends AnalysisElement {

	private static final long serialVersionUID = 1L;

	private static Image ICON = createImage(AnalysisDiagram.class,
			"icons/diagram16.gif"); //$NON-NLS-1$

	protected AnalysisRuler leftRuler, topRuler;

	private boolean rulersVisibility = false;
	private boolean snapToGeometryEnabled = false;
	private boolean gridEnabled = false;
	private double zoom = 1.0;

	protected List<AnalysisElement> children = new ArrayList<AnalysisElement>();

	public AnalysisDiagram() {
		super();
		createRulers();
	}

	protected void createRulers() {
		leftRuler = new AnalysisRuler(false);
		topRuler = new AnalysisRuler(true);
	}

	public void addChild(AnalysisElement child) {
		addChild(child, -1);
	}

	public void addChild(AnalysisElement child, int index) {
		if (index >= 0)
			children.add(index, child);
		else
			children.add(child);
		fireChildAdded(CHILDREN, child, new Integer(index));
	}

	public void removeChild(AnalysisElement child) {
		children.remove(child);
		fireChildRemoved(CHILDREN, child);
	}

	public List<AnalysisElement> getChildren() {
		return children;
	}

	public boolean getRulerVisibility() {
		return rulersVisibility;
	}

	public void setRulerVisibility(boolean rulersVisibility) {
		this.rulersVisibility = rulersVisibility;
	}

	public boolean isSnapToGeometryEnabled() {
		return snapToGeometryEnabled;
	}

	public void setSnapToGeometry(boolean snapToGeometryEnabled) {
		this.snapToGeometryEnabled = snapToGeometryEnabled;
	}

	public boolean isGridEnabled() {
		return gridEnabled;
	}

	public void setGridEnabled(boolean gridEnabled) {
		this.gridEnabled = gridEnabled;
	}

	public double getZoom() {
		return zoom;
	}

	public void setZoom(double zoom) {
		this.zoom = zoom;
	}

	public AnalysisRuler getRuler(int orientation) {
		AnalysisRuler result = null;
		switch (orientation) {
		case PositionConstants.NORTH:
			result = topRuler;
			break;
		case PositionConstants.WEST:
			result = leftRuler;
			break;
		}
		return result;
	}

	@Override
	public Image getIconImage() {
		return ICON;
	}

	@Override
	public Object clone() {
		AnalysisDiagram clone = new AnalysisDiagram();
		clone.rulersVisibility = rulersVisibility;
		clone.snapToGeometryEnabled = snapToGeometryEnabled;
		clone.gridEnabled = gridEnabled;
		clone.zoom = zoom;
		return clone;
	}

	@Override
	public PropertyHandler getPropertyHandler() {
		return new AnalysisDiagramProperties(this);
	}

}
